<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="120px">
          <el-form-item label="标准名称" prop="standardName">
            <el-input v-model="queryParams.standardName" placeholder="请输入实验教学名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="标准编号" prop="contentId">
            <el-input v-model="queryParams.contentId" placeholder="请输入实验编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item> -->
          <el-form-item label="学科" prop="standardSubject">
            <el-select v-model="queryParams.standardSubject" placeholder="请选择学科">
              <el-option v-for="dict in subjectOptions" :key="dict.id" :label="dict.subjectName" :value="dict.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年级" prop="standardGrade">
            <el-select v-model="queryParams.standardGrade" placeholder="年级" clearable>
              <el-option v-for="dict in sys_grade" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="学段" prop="standardPeriod">
            <el-select v-model="queryParams.standardPeriod" placeholder="请选择学段" clearable>
              <el-option v-for="dict in sys_period" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
         
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button type="primary" @click="handleAdd" v-hasPermi="['core:teachStandard:add']">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <h2>实验教学标准列表</h2>
      </template>

      <el-table v-loading="loading" :data="teachStandardList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标准编号" align="center" prop="id" />
        <el-table-column label="标准名称" align="center" prop="standardName" />

        <el-table-column label="学段" align="center" prop="standardPeriod">
          <template #default="scope">
            <dict-tag :options="sys_period" :value="scope.row.standardPeriod" />
          </template>
        </el-table-column>
        <el-table-column label="年级" align="center" prop="standardGrade">
          <template #default="scope">
            <dict-tag :options="sys_grade" :value="scope.row.standardGrade" />
          </template>
        </el-table-column>

        <el-table-column label="学科" align="center" prop="standardSubject">
          <template #default="scope">
            {{ getSubjectNameById(subjectOptions, scope.row.standardSubject) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleUpdate(scope.row)" v-hasPermi="['core:teachStandard:edit']">编辑</el-button>
            <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermi="['core:teachStandard:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="TeachStandard" lang="ts">
import { listTeachStandard, getTeachStandard, delTeachStandard, addTeachStandard, updateTeachStandard } from '@/api/core/teachStandard';
import { TeachStandardVO, TeachStandardQuery, TeachStandardForm } from '@/api/core/teachStandard/types';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_notice_type, sys_period, sys_grade } = toRefs<any>(proxy?.useDict('sys_notice_type', 'sys_period', 'sys_grade'));

//学科
import { getSubjectNameById } from '@/utils/index';
import { useSubjectList } from '@/composables/useSubjectList';
const { subjectOptions, fetchSubjectList } = useSubjectList();

const teachStandardList = ref<TeachStandardVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const viewDisabled = ref(false);
const queryFormRef = ref<ElFormInstance>();
const teachStandardFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: TeachStandardForm = {
  standardName: undefined,
  contentId: undefined,
  standardPeriod: undefined,
  standardSubject: undefined,
  attachmentList: undefined,
  content: undefined,
  standardGrade: undefined
};
const data = reactive<PageData<TeachStandardForm, TeachStandardQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    standardName: undefined,
    contentId: undefined,
    standardPeriod: undefined,
    standardSubject: undefined,
    attachmentList: undefined,
    content: undefined,
    standardGrade: undefined,
    params: {}
  },
  rules: {
    standardName: [{ required: true, message: '实验教学名称不能为空', trigger: 'blur' }],
    contentId: [{ required: true, message: '实验目录不能为空', trigger: 'blur' }],
    standardPeriod: [{ required: true, message: '学段不能为空', trigger: 'change' }],
    standardSubject: [{ required: true, message: '学科不能为空', trigger: 'change' }],
    standardGrade: [{ required: true, message: '年级不能为空', trigger: 'blur' }],
    content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询实验教学标准列表 */
const getList = async () => {
  loading.value = true;
  const res = await listTeachStandard(queryParams.value);
  teachStandardList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  teachStandardFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: TeachStandardVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({ path: '/experiment/teachStandard/panel', query: { type: 0 } });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: TeachStandardVO) => {
  const id = row?.id || ids.value[0];

  router.push({ path: '/experiment/teachStandard/panel', query: { id: id, type: 1 } });
};
/** 查看按钮操作 */
const handleView = async (row?: TeachStandardVO) => {
  const id = row?.id || ids.value[0];
  router.push({ path: '/experiment/teachStandard/panel', query: { id: id, type: 2 } });
};
/** 提交按钮 */
const submitForm = () => {
  teachStandardFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateTeachStandard(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addTeachStandard(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('修改成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: TeachStandardVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除实验教学标准编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delTeachStandard(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'core/teachStandard/export',
    {
      ...queryParams.value
    },
    `teachStandard_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
  fetchSubjectList();
});
</script>
